package maConfigration;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

import maMetrics.MetricCC;
import maMetrics.MetricCPD;
import maMetrics.MetricCountNonConstAttr;
import maMetrics.MetricDIT;
import maMetrics.MetricLOC;
import maMetrics.MetricNOC;
import maMetrics.MetricNOD;
import maMetrics.MetricNumberOfMethods;
import maMetrics.MetricObject;
import maMetrics.MetricsSizeOfStatechart;
import maUtil.LogFile;

public class CfgParser {
	private Collection<MetricObject> _metricsList = null;
	private ReadXMLConfig _config;
	private static String _sLogFolderName;
	
	public CfgParser(File fLogDir)
	{
		_sLogFolderName = creatLogFolder(fLogDir);
		if(null == _sLogFolderName)
			_sLogFolderName = creatLogFolder(null);
		LogFile.setPath(_sLogFolderName);
		writeFolderName(null);
		_config = new ReadXMLConfig();
		
		//Collection isAppliedToProj = _config.isAppliedTo("isApplyToProj");
		//Collection isAppliedToPkg = _config.isAppliedTo("isApplyToPkg");
		//Collection isAppliedToCls = _config.isAppliedTo("isApplyToCls");
		//Collection isAppliedToOp = _config.isAppliedTo("isApplyToOp");
		
		_metricsList = new ArrayList<MetricObject>();
		MetricSettings ms;
		ms = _config.getMetricSettings("CC");
		_metricsList.add(new MetricCC(ms));
		ms = _config.getMetricSettings("LOC");
		_metricsList.add(new MetricLOC(ms));
		ms = _config.getMetricSettings("SOS");
		_metricsList.add(new MetricsSizeOfStatechart(ms));
		ms = _config.getMetricSettings("NCA");
		_metricsList.add(new MetricCountNonConstAttr(ms));
		ms = _config.getMetricSettings("DIT");
		_metricsList.add(new MetricDIT(ms));
		ms = _config.getMetricSettings("NOC");
		_metricsList.add(new MetricNOC(ms));
		ms = _config.getMetricSettings("NOD");
		_metricsList.add(new MetricNOD(ms));
		ms = _config.getMetricSettings("NOM");
		_metricsList.add(new MetricNumberOfMethods(ms));
		ms = _config.getMetricSettings("CPD");
		_metricsList.add(new MetricCPD(ms));

	}
	
	public Collection<MetricObject> getMetricsList()
	{
		return _metricsList;
	}
	
	public Collection<String> getProjectsToBeRun()
	{
		return _config.getProjectsToBeRun();
	}
	
	public void addMetric(MetricObject mo)
	{
		_metricsList.add(mo);
	}
	
	public Collection getFilter(String str){
		return _config.getFilter(str);
	}
	private static String creatLogFolder(File fLogDir)
	{
		Date date = new Date();
		String sFolderName = null;
		if(fLogDir != null)
		{
			sFolderName = fLogDir.getAbsolutePath() + "\\";
		}
		else
		{
			sFolderName = "log\\";
		}
		SimpleDateFormat ft = 
			       new SimpleDateFormat ("yyMMddHHmm");
		String tem = ft.format(date);
		sFolderName += tem;
		LogFile.setFolderName(tem);
		File file = new File(sFolderName);
		file = file.getAbsoluteFile();
		try{
			if(file.mkdirs())
				return file.getAbsolutePath();
			else return null;
		}
		catch(Exception e)
		{
			System.out.println("Failed to create folder:" + sFolderName);
			System.out.println(e.getMessage());
			return null;
		}
		
	}
	
	public static String getLogFolderName()
	{
		return _sLogFolderName;
	}
	
	public static boolean writeFolderName(String sPathName)
	{
		boolean ret = false;
		File file;
		String content = LogFile.getFolderName() + "\r\n";
		if(sPathName == null) sPathName = _sLogFolderName;
		FileOutputStream fop = null;
 
		try {
			File f = new File(sPathName);
			file = new File(f.getParent() + "\\FolderList.csv");
			// if file doesnt exists, then create it
			if (!file.exists()) {
				file.createNewFile();
				content = "FolderNames\r\n" + content;
			}
			fop = new FileOutputStream(file, true);
			// get the content in bytes
			byte[] contentInBytes = content.getBytes();
			fop.write(contentInBytes);
			fop.flush();
			fop.close();
			ret = true;
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("Failed to open FolderList.csv.");
		} finally {
			try {
				if (fop != null) {
					fop.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		
		return ret;
	}

}
